<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <title>购物车</title>
  <link rel="stylesheet" th:href="@{/css/cart.css}" />
</head>
<body>
<header>
  <h1>购物车</h1>
  <nav>
    <a th:href="@{/}" class="btn-link">返回首页</a>
  </nav>
</header>

<main>
  <section class="message" th:if="${msg != null}">
    <p th:text="${msg}"></p>
  </section>

  <section th:if="${cartItems != null and !cartItems.isEmpty()}">
    <table class="cart-table">
      <thead>
      <tr>
        <th>书名</th>
        <th>作者</th>
        <th>单价 (元)</th>
        <th>数量</th>
        <th>小计 (元)</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <tr th:each="item : ${cartItems}">
        <td th:text="${item.getBook() != null ? item.getBook().getTitle() : '已下架'}"></td>
        <td th:text="${item.getBook() != null ? item.getBook.getAuthor() : '未知'}"></td>
        <td th:text="${item.getBook != null ? #numbers.formatDecimal(item.getBook().getPrice(), 1, 2) : '0.00'}"></td>
        <td th:text="${item.getCart().getQuantity()}"></td>
        <td th:text="${item.getBook() != null ? #numbers.formatDecimal(item.getSubTotal(), 1, 2) : '0.00'}"></td>
        <td>
          <form th:action="@{/cart/remove}" method="post" class="inline-form">
            <input type="hidden" name="cartId" th:value="${item.getCart().getId()}" />
            <button type="submit" class="btn-remove" onclick="return confirm('确定要移除吗？')">移除</button>
          </form>
        </td>
      </tr>
      </tbody>
    </table>

    <div class="total-section">
      <h3>总价: <span th:text="${#numbers.formatDecimal(totalPrice, 1, 2)}"></span> 元</h3>
      <form th:action="@{/order/checkout}" method="post">
        <button type="submit" class="btn-checkout">去结算</button>
      </form>
    </div>
  </section>

  <section th:if="${cartItems == null or cartItems.isEmpty()}" class="empty-cart">
    <p>购物车为空。</p>
  </section>
</main>

<footer>
  <p>书店系统 &copy; 2025</p>
</footer>
</body>
</html>
